package com.wisdomparking.control.data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wisdomparking.bean.Orders;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.ToolService;

/**
 * 对外服务接口
 */
@RestController
@RequestMapping("/data/serviceCenter")
public class ServiceController extends BaseController{
	
	@Autowired OrdersService ordersService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	@Autowired RawdataService rawdataService;
	
	/**
	 * 生成订单 - 抬杆通知接口【<<====】
	 * 
	 * 车辆进入停车场，由停车场发出指令调用此接口，在平台生成订单
	 * 
	 * @param plateNumber		车牌
	 * @param identification		车场唯一标识
	 * @param memberFlag			是否车场会员标记
	 * @return
	 */
	@RequestMapping("/generateOrder")
	public Result<Orders> generateOrder(String plateNumber, String identification, Boolean memberFlag) {
		
		Orders order = ToolService.generateOrder(ordersService, vehicleInfoService, parkingService, plateNumber,
				identification, memberFlag);

		return ResultGenerator.genSuccessResult(order);
	}

	/**
	 * 结束订单【<<====】
	 * 
	 * 车场现场现金支付时，调用完成平台订单
	 * 
	 * @param plateNumber
	 *            车牌号
	 * @param identification
	 *            车场唯一标识
	 * @param paymentAmount
	 *            支付金额
	 * @param payMode
	 *            支付方式 [非必要]
	 * @return
	 */
	@RequestMapping("/closureOrder")
	public Result<Boolean> closureOrder(String plateNumber, String identification, String paymentAmount,
			@RequestParam(value = "payMode", required = false, defaultValue = "现金") String payMode) {
		
		boolean flag = ToolService.closureOrder(ordersService, plateNumber, identification, paymentAmount, payMode);
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	
	
//	/**
//	 * 车场实时信息 - 心跳数据【<<====】
//	 * 
//	 * @param map	其中必须包含车场唯一标识: identification
//	 * @return
//	 */
//	@SuppressWarnings("unchecked")
//	@RequestMapping("/heartbeatPackets")
//	public Boolean heartbeatPackets(String realTimeData){
//		boolean flag = false;
//		
//		if(ToolValid.isNotEmpty(realTimeData)){
//			
//			Map<String, Object> map = new HashMap<String, Object>();
//			map = new Gson().fromJson(realTimeData, map.getClass());
//			
//			String identification = String.valueOf(map.get("identification"));
//			if(ToolValid.isNotEmpty(identification)){
//				boolean mark = false;
//				Rawdata rawdata = rawdataService.getRawdataByIdentification(identification);
//				
//				if(ToolValid.isNull(rawdata)){
//					rawdata = new Rawdata();
//					mark = true;
//				}
//				
//				rawdata.setIdentification(identification);
//				rawdata.setPutInStorageMark(false);
//				rawdata.setRealTimeData(new Gson().toJson(map));
//				rawdata.setUpdateTime(new Date());
//				
//				if(mark){
//					flag = rawdataService.addRawdata(rawdata, true);
//				}
//				else{
//					flag = rawdataService.updateRawdata(rawdata, true);
//				}
//				
//			}
//		}
//		return flag;
//	}
//	
//	/**
//	 * 通过 App 支付完成后通知车场做抬杠动作【====>>】
//	 * 
//	 * @param identification	车场唯一标识
//	 * @param plateNumber		车牌号
//	 */
//	@RequestMapping("/payfinishedSendParking")
//	public Boolean payfinishedSendParking(String identification, String plateNumber){
//		
//		return ToolService.payfinishedSendParking(parkingService, identification, plateNumber);
//	}
	
}
